package com.ruoyi.reception;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.receipt.domain.TbUser;
import com.ruoyi.receipt.domain.TbUserMessage;
import com.ruoyi.receipt.domain.TbUserRelation;
import com.ruoyi.receipt.service.ITbUserMessageService;
import com.ruoyi.receipt.service.ITbUserRelationService;
import com.ruoyi.receipt.service.ITbUserService;
import com.ruoyi.receipt.utils.CurrentUserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户消息Controller
 * 
 * @author ruoyi
 * @date 2021-03-30
 */
@RestController
@RequestMapping("/reception/message")
public class TbUserMessageRecController extends BaseController
{
    @Autowired
    private ITbUserMessageService tbUserMessageService;

    @Autowired
    private ITbUserService tbUserService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private ITbUserRelationService tbUserRelationService;

    /**
     * 发送绑定消息
     */
    @PostMapping("/sendBandMessage")
    public AjaxResult sendBandMessage(@RequestBody Map map)
    {
        String sendUserOperation=map.get("sendUserOperation")+"";

        TbUser currentUser = CurrentUserUtils.getCurrentUser();

        TbUserMessage tbUserMessage=new TbUserMessage();
        tbUserMessage.setId(idWorker.nextId());
        tbUserMessage.setSendUserId(currentUser.getId());
        tbUserMessage.setSendUserLoginAccount(currentUser.getLoginAccount());
        tbUserMessage.setSendUserName(currentUser.getRealName());
        tbUserMessage.setSendUserOperation(Integer.parseInt(sendUserOperation));

        String bandPhone=map.get("bandPhone")+"";
        TbUser tbUser=new TbUser();
        tbUser.setLoginAccount(bandPhone);
        tbUser.setIsDel(0);

        //如果是家长的话绑定学生
        if(currentUser.getType()==1){
            tbUser.setType(2);
        }

        //如果是学生的话绑定家长
        if(currentUser.getType()==2){
            tbUser.setType(1);
        }

        List<TbUser> tbUsersList = tbUserService.selectTbUserList(tbUser);
        if(tbUsersList.size()==0){
            throw new CustomException("绑定账号不存在");
        }

        TbUser dbUser=tbUsersList.get(0);

        tbUserMessage.setReceiptUserId(dbUser.getId());
        tbUserMessage.setReceiptUserOperation(0);
        tbUserMessage.setIsDel(0);
        tbUserMessage.setSendUserType(currentUser.getType());
        tbUserMessageService.insertTbUserMessage(tbUserMessage);
        return AjaxResult.success();
    }

    /**
     * 确认消息
     */
    @PostMapping("/questionMessage")
    public AjaxResult questionMessage(@RequestBody Map map)
    {
        Integer receiptUserOperation=Integer.parseInt(map.get("receiptUserOperation")+"");
        Long tbUserMessageId=Long.parseLong(map.get("id")+"");


        TbUserMessage tbUserMessage = tbUserMessageService.selectTbUserMessageById(tbUserMessageId);
        tbUserMessage.setReceiptUserOperation(receiptUserOperation);

        //进行用户绑定
        if(receiptUserOperation==1){
            Integer sendUserType = tbUserMessage.getSendUserType();
            TbUserRelation tbUserRelation=new TbUserRelation();
            tbUserRelation.setParentId(sendUserType==1?tbUserMessage.getSendUserId():tbUserMessage.getReceiptUserId());
            tbUserRelation.setChildId(sendUserType==2?tbUserMessage.getReceiptUserId():tbUserMessage.getSendUserId());
            tbUserRelation.setIsDel(0);

            //判断以前是否绑定过
            if(tbUserRelationService.selectTbUserRelationList(tbUserRelation).size()==0){
                tbUserRelation.setId(idWorker.nextId());
                tbUserRelationService.insertTbUserRelation(tbUserRelation);
            }

        }

        //进行用户解绑
        if(receiptUserOperation==2){
            Integer sendUserType = tbUserMessage.getSendUserType();
            TbUserRelation tbUserRelation=new TbUserRelation();
            tbUserRelation.setParentId(sendUserType==1?tbUserMessage.getSendUserId():tbUserMessage.getReceiptUserId());
            tbUserRelation.setChildId(sendUserType==2?tbUserMessage.getReceiptUserId():tbUserMessage.getSendUserId());
            List<TbUserRelation> tbUserRelations = tbUserRelationService.selectTbUserRelationList(tbUserRelation);
            if(tbUserRelations.size()>0){
                tbUserRelationService.deleteTbUserRelationById(tbUserRelations.get(0).getId());
            }
        }

        tbUserMessageService.updateTbUserMessage(tbUserMessage);

        return AjaxResult.success();
    }

    /**
     * 修改用户消息
     */
    @PreAuthorize("@ss.hasPermi('receipt:message:edit')")
    @Log(title = "用户消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbUserMessage tbUserMessage)
    {
        return toAjax(tbUserMessageService.updateTbUserMessage(tbUserMessage));
    }

    /**
     * 删除用户消息
     */
    @PreAuthorize("@ss.hasPermi('receipt:message:remove')")
    @Log(title = "用户消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbUserMessageService.deleteTbUserMessageByIds(ids));
    }
}
